package com.zgscwjm.taobaphelper.auto;

import android.content.Context;
import android.util.Log;

import com.zgscwjm.taobaphelper.BaseConfig;
import com.zgscwjm.taobaphelper.exutils.LsfbLog;
import com.zgscwjm.taobaphelper.exutils.StringUtils;
import com.zgscwjm.taobaphelper.xp.FileManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zgscwjm on 17-3-9.
 * 对应原来的b类
 */

public class updateCookie {

    // FIXME: 17-3-9 这个a没找到哪里在用
    public static Object a =new Object();
    public static String myCookie = com.zgscwjm.taobaphelper.exutils.StringUtils.EMPTY;
    public static String[] c = new String[]{"www.154158.com","wx.tianxiaomao.com","kb.sutuijingling.com"};

    // FIXME: 17-3-9 这个d没找到哪里在用
    public static final SimpleDateFormat d  = new SimpleDateFormat("yy-MM-dd HH:mm:ss");

    // FIXME: 17-3-9 这个e没找到哪里在用
    static Context e = null;


    /**
     * 这个方法应该更新cookeis  对应原来的a
     * @param context
     * @param url
     * @param map
     * @return
     */
    public static String updateCookie(Context context, String url, Map map){
        if(!(url.startsWith(BaseConfig.baseUrl)||url.startsWith("http://154158.com/")||url.startsWith("http://115.159.25.42:9003/"))){
            url = BaseConfig.baseUrl + url;
        }
        LsfbLog.e("DS","url["+url+"]:"+map);

        if (map==null){
            map = new HashMap();
        }
        //  这里有神坑.DefaultHttpClient已经被Apache取消了,单独下了一个包,有可能会存在冲突
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
        List arrayList = new ArrayList();
        HttpConnectionParams.setSoTimeout(defaultHttpClient.getParams(),5000);
        HttpConnectionParams.setConnectionTimeout(defaultHttpClient.getParams(),5000);
        defaultHttpClient.getParams().setParameter("http.protocol.allow-cirular-redirects",Boolean.valueOf(true));
        defaultHttpClient.getParams().setParameter("http.protocol.max-redirects",Integer.valueOf(20));
        new c(map,arrayList).a();
        HttpUriRequest httpPost = new HttpPost(url);
        try{
            //// FIXME: 17-3-9 这里entity目前导入的包里面没有这个方法.
//            if (!arrayList.isEmpty()){
//                httpPost.setEntity(new UrlEncodedFormEntity(arrayList, "UTF-8"));
//            }

            if (StringUtils.isEmpty(myCookie) && context!=null){
                myCookie = FileManager.findDBInfo(context,FileManager.CookiesLoginParam);

            }

            if (!StringUtils.isEmpty(myCookie)){
                httpPost.setHeader("Cookie","_sid_="+ myCookie);
            }

            HttpResponse execute = defaultHttpClient.execute(httpPost);
            int statusCode = execute.getStatusLine().getStatusCode();
            if (statusCode == 301 || statusCode == 302){
                execute.getHeaders("Location");
            }

            HttpEntity entity = execute.getEntity();
            String entityUtils = EntityUtils.toString(entity);
            entity.consumeContent();
            List cookies = defaultHttpClient.getCookieStore().getCookies();
            for (statusCode =0;statusCode<cookies.size();statusCode++){
                if ("_sid_".equals(((Cookie)cookies.get(statusCode)).getName())){
                  myCookie = ((Cookie)cookies.get(statusCode)).getValue();
                  if (context!=null){
                      FileManager.saveDBInfo(context,FileManager.CookiesLoginParam, myCookie);
                  }
                    LsfbLog.e("_sid_", myCookie);
                    LsfbLog.e("result",entityUtils);
                    return entityUtils;
                }
            }
            Log.e("result",entityUtils);
            return entityUtils;
        }catch (Exception e){
            LsfbLog.e(e);
            return  null;
        }

    }

    public static String a(String str, Map map) {
        return updateCookie(null, str, map);
    }


    /**
     * 对应原来的a
     */
    public static final void sleep1000() {
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 对应原来的b
     */
    public static final void sleep2000() {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 对应原来的c
     */
    public static final void sleep3000() {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 对应原来的d
     */
    public static final void sleep5000() {
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 对应原来的e
     */
    public static final void sleep7000() {
        try {
            Thread.sleep(7000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 对应原来的f
     */
    public static final void sleep10000() {
        try {
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
